<template>
  <div class="app-container">
    <div class="mainbox">
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm">
          <el-form-item label="上游系统" prop="systemName">
            <el-select v-model.trim="filterForm.systemName" filterable placeholder="请选择">
              <el-option
                v-for="(item) in systemList"
                :key="item.systemId"
                :label="item.systemName"
                :value="item.systemName"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </div>
      <dt-table
        ref="dtTable"
        v-loading="loading"
        :table-opts="tableOpts"
        row-key="id"
        :pagination-data="{
          total: total,
          queryParams: filterForm
        }"
        @pagination="getList"
      >
      </dt-table>
    </div>
    <UpstreamFormModal ref="UpstreamFormModal" @success="refresh" />
    <UpstreamTableModal ref="UpstreamTableModal" />
    <personSelect
      v-if="allocationShow"
      ref="personPicker"
      v-model="personValue"
      :params="params"
      :search-user-params="params"
      :api="bussinessRoleManageApi"
      :selectedData="[]"
      :tag-visible="false"
      :reappear="true"
      :selected-data="select"
      @handleSelectData="selected" />
  </div>
</template>

<script>
import UpstreamFormModal from './components/UpstreamFormModal'
import UpstreamTableModal from './components/UpstreamTableModal'
import { getSystemName, getList, deleteVideoSystem, getRoleId, allocationPerson, getPerson, countSystemAndEquipment } from '@/api/videoFusion/upstreamAccess'
import { getManagingOrgUserList, searchOrgName, searchUserName } from '@/framework/api/authCenter/bussinessRoleManage'
import { getUserListByUserIds } from '@/framework/api/affairManage/flowDetail/Config'

export default {
  components: { UpstreamFormModal, UpstreamTableModal },
  data() {
    return {
      bussinessRoleManageApi: { getManagingOrgUserList, searchOrgName, searchUserName, getUserListByUserIds },
      params: {
        roleId: ''
      },
      personValue: '',
      select: [],
      selectSystemId: '',
      total: 0,
      filterForm: {
        systemName: '',
        pageNo: 1,
        pageSize: 10
      },
      systemList: [],
      tableOpts: {
        rowKey: 'systemId',
        toolbarBtn: [
          {
            label: '新增',
            permission: ['VIDEO_UPSTREAM_ADD_BUTTON'],
            type: 'primary',
            icon: 'el-icon-plus',
            click: () => {
              this.handleAdd()
            }
          }
        ],
        tableData: [],
        firstTableCol: {
          select: false
        },
        indexTableCol: {
          index: true
        },
        tableCol: [
          {
            prop: 'systemName',
            label: '上游系统',
            show: true
          },
          {
            prop: 'describeInfo',
            label: '描述',
            show: true
          },
          {
            prop: 'createUserName',
            label: '创建人',
            show: true
          },
          {
            prop: 'createTime',
            label: '创建时间',
            show: true
          }
        ],
        operator: {
          width: 292,
          operatorBtn: [
            {
              text: '修改',
              permission: ['VIDEO_UPSTREAM_EDIT_BUTTON'],
              size: 'small',
              click: (row, col, cellValue) => { return this.editInfo(row) }
            },
            {
              text: '分配管理人员',
              permission: ['VIDEO_UPSTREAM_ALLOCATION_BUTTON'],
              size: 'small',
              click: (row, col, cellValue) => { return this.allocation(row) }
            },
            {
              text: '删除',
              permission: ['VIDEO_UPSTREAM_DEL_BUTTON'],
              size: 'small',
              type: 'danger',
              click: (row, col, cellValue) => { return this.remove(row) }
            }
          ]
        }
      },
      loading: false,
      allocationShow: false
    }
  },
  mounted() {
    this.refresh()
    getRoleId().then((res) => {
      this.params.roleId = res.data
    })
  },
  methods: {
    selected(select) {
      this.select = select
      const params = {
        systemId: this.selectSystemId
      }
      params.userIds = this.select.map((item) => {
        return item.userId
      })
      allocationPerson(params).then((res) => {
        this.$dtModal.msgSuccess(res.message)
        this.selectSystemId = ''
        this.personValue = ''
        this.allocationShow = false
      })
    },
    allocation(row) {
      this.allocationShow = false
      this.selectSystemId = row.systemId
      this.personValue = ''
      if (this.params.roleId) {
        setTimeout(() => {
          this.allocationShow = true
          getPerson(row.systemId).then((res) => {
            this.personValue = res.data.map((item) => {
              return item.userId
            }).join(',')
            setTimeout(() => {
              this.$refs.personPicker.show()
            }, 10)
          })
        }, 10)
      }
    },
    refresh() {
      this.getSystemList()
      this.getList()
    },
    editInfo(row) {
      this.$refs.UpstreamFormModal.action = 'edit'
      this.$refs.UpstreamFormModal.edit(row)
    },
    handleAdd() {
      this.$refs.UpstreamFormModal.action = 'add'
      this.$refs.UpstreamFormModal.add()
    },
    remove(row) {
      countSystemAndEquipment({ systemId: row.systemId }).then((res) => {
        if (res.data.systemNum > 0) {
          this.$dtModal.msgWarning('您选择的上游系统存在监控信息，不能删除')
        } else {
          this.$dtModal.confirm('是否删除选择的上游系统？').then(() => {
            return deleteVideoSystem({
              systemId: row.systemId,
              systemIds: [row.systemId]
            })
          }).then(() => {
            this.refresh()
            this.$dtModal.msgSuccess('删除成功')
          }).catch(() => {})
        }
      })
    },
    // 搜索
    handleFilter() {
      this.filterForm.pageNo = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.$refs.filterFormRef.resetFields('filterForm')
      this.handleFilter()
    },
    getList() {
      this.loading = true
      getList(this.filterForm).then((res) => {
        this.tableOpts.tableData = res.data.rows
        this.total = res.data.totalRows
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    getSystemList() {
      getSystemName().then((res) => {
        this.systemList = res.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
